package com.ztgf.commodity.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.commodity.models.bo.BrandInfoBO;
import com.ztgf.commodity.models.bo.BrandInfoSaveBO;
import com.ztgf.commodity.models.dto.BrandInfoDTO;
import com.ztgf.commodity.models.enums.EPGCodeEnum;
import com.ztgf.commodity.models.service.IBrandInfoService;
import com.ztgf.commodity.models.vo.BrandInfoVO;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.PinYinUtils;
import com.ztgf.common.utils.StrUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * author: chenyu.wang
 * date: 2020-05-25 13:57
 * Description:
 */
@RestController
@RequestMapping("/cms/brandInfo")
@Slf4j
@Api(value = "BrandInfoController", tags = "商品品牌表接口")
public class BrandInfoController {

    @DubboReference
    private IBrandInfoService brandInfoService;

    @ApiOperation(value = "添加商品品牌表", notes = "添加商品品牌表")
    @PostMapping(value = "/insert")
    public Result insertBrandInfo(BrandInfoSaveBO saveBO) {
        if (brandInfoService.updateByName(saveBO.getName())) {
            return new Result<>(CodeEnum.FAIL_100043);
        }
        boolean flag = brandInfoService.addBrand(saveBO);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @ApiOperation(value = "修改商品品牌表", notes = "修改商品品牌表")
    @PostMapping("/update")
    public Result updateBrandInfo(BrandInfoSaveBO saveBO) {
        BrandInfoDTO brandInfoDTO = new BrandInfoDTO();
        BeanUtils.copyProperties(saveBO, brandInfoDTO);
        brandInfoDTO.setId(saveBO.getId());
        brandInfoDTO.setInitials(PinYinUtils.getPinYinHeadCharFirstBrand(saveBO.getName()));
        boolean flag = brandInfoService.updateById(brandInfoDTO);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @ApiOperation(value = "删除商品品牌表(物理删除)", notes = "删除商品品牌表(物理删除)")
    @ApiImplicitParam(name = "id", value = "商品品牌表id", required = true, paramType = "query", dataType = "Long")
    @PostMapping("/delete")
    public Result delete(Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        boolean flag = brandInfoService.deleteById(id);
        if (flag) {
            return new Result<>();
        }
        return new Result<>(CodeEnum.FAIL_100009);
    }

    @ApiOperation(value = "删除/恢复商品品牌表(逻辑删除)", notes = "删除/恢复商品品牌表(逻辑删除)")
    @ApiImplicitParam(name = "id", value = "商品品牌表id", required = true, paramType = "query", dataType = "Long")
    @PostMapping("/deleteLogic")
    public Result deleteLogic(Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        boolean flag = brandInfoService.deleteLogic(id);
        if (flag) {
            return new Result<>();
        }
        return new Result<>(CodeEnum.FAIL_100009);

    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除商品类目表", notes = "批量删除商品类目表")
    @ApiImplicitParam(name = "ids", value = "ids:以逗号隔开", required = true, paramType = "query", dataType = "String")
    public Result deleteBatch(String ids) {
        if (StrUtils.isEmpty(ids)) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        String[] arr = ids.split(",");
        brandInfoService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/getById")
    @ApiOperation(value = "根据id查找商品品牌表", notes = "根据id查找商品品牌表")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<BrandInfoDTO> getById(Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        return new Result<>(brandInfoService.selectById(id));
    }

    @ApiOperation(value = "商品品牌列表", notes = "商品品牌列表")
    @PostMapping("/queryPageList")
    public Result<Page<BrandInfoVO>> queryPageList(BrandInfoBO brandInfoBO) {
        Page<BrandInfoVO> page = new Page<>(brandInfoBO.getPageNo(), brandInfoBO.getPageSize());
        page = brandInfoService.queryPageList(page, brandInfoBO);
        return new Result<>(page);
    }

    @ApiOperation(value = "商品品牌列表排序", notes = "商品品牌列表排序")
    @PostMapping("/queryPageOrderList")
    public Result<Page<BrandInfoDTO>> queryPageBySortList(BrandInfoBO brandInfoBO) {
        Wrapper<BrandInfoDTO> wrapper = new EntityWrapper<>();
        Result result = new Result();
        if (null == brandInfoBO.getSortType()) {
            result.setCode(CodeEnum.FAIL.getCode());
            result.setData("传入排序规则为空");
            return result;
        }
        if (brandInfoBO.getSortType().equals(EPGCodeEnum.SORT_ASC.getCode())) {
            wrapper.orderBy("sort", true);
        } else {
            wrapper.orderBy("sort", false);
        }
        Page<BrandInfoDTO> page = new Page<>(brandInfoBO.getPageNo(), brandInfoBO.getPageSize());
        page = brandInfoService.selectPage(page);
        return new Result<>(page);
    }

    @PostMapping("/getByName")
    @ApiOperation(value = "根据品牌名称判断是否存在", notes = "根据品牌名称判断是否存在")
    public Result getByName(String name) {
        if (name == null || "".equals(name)) {
            return new Result(CodeEnum.FAIL_100002);
        }

        if (brandInfoService.updateByName(name)) {
            return new Result<>(CodeEnum.FAIL_100043);
        } else {
            return new Result<>();
        }
    }

    @PostMapping("/updateIsPush")
    @ApiOperation(value = "修改品牌是否发布", notes = "修改品牌是否发布")
    public Result updateIsPush(Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        BrandInfoDTO select = brandInfoService.selectById(id);
        if (select == null) {
            return new Result<>(CodeEnum.FAIL_COMMODITY_ERROR_340000);
        }
        BrandInfoDTO updateDto = new BrandInfoDTO();
        updateDto.setId(select.getId());
        if (select.getIsPublish() == 1) {
            updateDto.setIsPublish(0);
        } else {
            updateDto.setIsPublish(1);
        }
        boolean flag = brandInfoService.updateById(updateDto);
        if (flag) {
            return new Result<>(CodeEnum.SUCCESS);
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }
}
